import unittest 


from btreePlus import Pager
class TestPages(unittest.TestCase):
    def test0Use(self):
        pager = Pager("tbl_order.db", 0x400*0x400, 0x400)
        self.assertIsNotNone(pager)
        self.assertEqual(pager.pageSize(), 0x400)
        self.assertEqual(0, pager.open())   # create Root Page with Zero
        pager.close()  # file size == PageSize

    def test1WritePage(self):
        pager = Pager("tbl_order.db", 0x400*0x400, 0x400)
        pager.open()

        pageData = 0x400*'a'
        firstPage = pager.allocPage(1)
        self.assertTrue(firstPage.isAllocated())

        firstPage.writeString(pageData);
        self.assertTrue(firstPage.isDirty())

        pager.writePage(firstPage)
        self.assertTrue(firstPage.isDirty())

        pager.savePoint()
        self.assertTrue(firstPage.isSync())

        #2. alloc & write & save page 2  
        second = pager.allocPage(2)
        self.assertTrue(second.isAllocated())

        second.writeString(1024*'b')
        self.assertTrue(second.isDirty())

        pager.writePage(second)
        self.assertTrue(second.isDirty())

        pager.savePoint()
        self.assertTrue(second.isSync())

        #2. read cached Page 2
        second = pager.readPage(2);
        self.assertTrue(second.isSync())

        second.writeString(1024*'c')
        self.assertTrue(second.isDirty())

        pager.writePage(second)
        self.assertTrue(second.isDirty())

        pager.savePoint()
        self.assertTrue(second.isSync())

        pager.close()  # file size == PageSize
    
    def test2ReadPage(self):
        pager = Pager("tbl_order.db", 0x400*0x400, 0x400)
        self.assertEqual(0, pager.open())
        first = pager.readPage(1)
        self.assertTrue(first.isSync())

        first.writeString(1024*'d')
        self.assertTrue(first.isDirty())

        pager.writePage(first)
        self.assertTrue(first.isDirty())

        pager.savePoint()
        self.assertTrue(first.isSync())

        pager.close()  # file size == PageSize



from btreePlus import Cache
class TestCache(unittest.TestCase):
    def testOnePage(self):
        ca = Cache(0x1*0x400, 0x400)
        self.assertEqual(0, ca.init())
        ca.deinit()
    def testUse(self):
        ca = Cache(0x4*0x400, 0x400)
        self.assertEqual(0, ca.usedPageCount())
        self.assertEqual(0, ca.freePageCount())

        self.assertEqual(0, ca.init())
        self.assertEqual(0, ca.usedPageCount())
        self.assertEqual(0x4, ca.freePageCount())


        page = ca.acquire(1)
        self.assertIsNone(page)

        page = ca.allocate(1)
        self.assertIsNotNone(page)
        self.assertEqual(1, ca.usedPageCount())
        self.assertEqual(3, ca.freePageCount())

        page = ca.acquire(1)
        self.assertIsNotNone(page)

        self.assertEqual(0, ca.release(page))
        self.assertEqual(0, ca.usedPageCount())
        self.assertEqual(0x4, ca.freePageCount())

        ca.deinit()
        self.assertEqual(0, ca.usedPageCount())
        self.assertEqual(0, ca.freePageCount())


from btreePlus import Page
class TestPage(unittest.TestCase):
    def testUse(self):
        page = Page.newPage(0x400)
        page.writeString(0x400*"p")
        #page.readChar(0)
        Page.freePage(page)

#from btreePlus import BtreePlus
#class TestBtreePlus(unittest.TestCase):
#    def testBtree():
#        b = BtreePlus("tbl_order")
#        print(str(b))

from btreePlus import UnitTest
if __name__ == "__main__":
    UnitTest().run()

    suite = unittest.TestSuite([
          unittest.TestLoader().loadTestsFromTestCase(TestPage),
          unittest.TestLoader().loadTestsFromTestCase(TestPages),
          unittest.TestLoader().loadTestsFromTestCase(TestCache),
              ])
    unittest.TextTestRunner(verbosity=2).run(suite)
